//
//  SNChartSelectTypeView.m
//  project
//
//  Created by 黄建华 on 2017/11/29.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "SNChartSelectTypeView.h"

@implementation SNChartSelectTypeView

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        NSArray *infoArray = [NSArray arrayWithObjects:SN_Text(@"本周"), SN_Text(@"本月"), SN_Text(@"年"), nil];
        for (int i = 0; i < 3; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = SNFRAME(self.width / 3 *i, 0, self.width / 3, self.height);
            [button addTarget:self
                       action:@selector(buttonclick:)
             forControlEvents:UIControlEventTouchUpInside];
            button.tag = i + 101;
            [button setTitleColor:SNCOLORBLACK forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            [button setTitle:[infoArray objectAtIndex:i] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            [self addSubview:button];
            button.layer.borderWidth = 0.5;
            button.layer.borderColor = SNCOLORBLACK.CGColor;
            
            if (i == 0) {
                button.selected = YES;
                [button setBackgroundColor:SNCOLORBLACK];
            } else {
                button.selected = NO;
                [button setBackgroundColor:[UIColor whiteColor]];
            }
        }
    }
    return self;
}

- (void)buttonclick:(UIButton *)sender {
    SelectDateType type = SelectDateWeek;
    
    //     if (sender.tag - 100 == 1)
    //     {
    //        type = ChartDateWeek;
    //     }
    //     if (sender.tag - 100 == 2)
    //     {
    //         type = ChartDateMonth;
    //     }
    //     if (sender.tag - 100 == 3)
    //     {
    //        type = ChartDateYear;
    //     }
    for (UIButton *button in self.subviews) {
        button.selected = NO;
        [button setBackgroundColor:[UIColor whiteColor]];
    }
    if (sender.tag - 100 == 1) {
        type            = SelectDateWeek;
        sender.selected = YES;
        [sender setBackgroundColor:SNCOLORBLACK];
    }
    if (sender.tag - 100 == 2) {
        type            = SelectDateMonth;
        sender.selected = YES;
        [sender setBackgroundColor:SNCOLORBLACK];
    }
    if (sender.tag - 100 == 3) {
        type            = SelectDateYear;
        sender.selected = YES;
        [sender setBackgroundColor:SNCOLORBLACK];
    }
    
    if (_selectType) {
        _selectType(type);
    }
}

@end

